<?php
if (!defined("_INDEX")) { header("Location: /"); die; }

$errors = array();

if (isset($_POST['action']) && $_POST['action'] == 'add')
{
	$city = UV::readInt($route->POST("city"));
	$name = UV::readMQ($route->POST("name"));
	$address = UV::readMQ($route->POST("address"));
	$phone = UV::readMQ($route->POST("phone"));
	$about = UV::readMQ($route->POST("about"));
	$donor = trim(UV::readMQ($route->POST("donor")));
	$cookie = trim(UV::readMQ($route->POST("cookie")));
	
	$query = "insert into `brokers` values(NULL,'$city','$name','$address','$phone','$about','$donor','$cookie')";
	
	$update = $db->sendQuery($query);
	if ($update) {
		$broker_id = mysql_insert_id();
		
		$companies = array();
		if ($route->POST("companies")){
			foreach ($route->POST("companies") as $c){
				if (!in_array($c,$companies)){
					$companies[] = $c;
				}
			}
		}
		
		if (count($companies) > 0){
			$query = "insert into `partners` values ";
			foreach ($companies as $key=>$company){
				$query .= "(NULL,'$broker_id','$company')";
				if ($key < count($companies) - 1)
					$query .= ",";
			}
			
			if (!$db->sendQuery($query))
				$errors[] = "Ошибка добавления партнеров";
		}
	} else {
		$errors[] = "Ошибка добавления записи";
	}
	
	if (count($errors) == 0)
		goToPage("/admin/brokers/edit/$broker_id");
}

loadPage("head.php");
?>
<script type='text/javascript'>
$(document).ready(function(){
	$("#add_company").click(function(){
		var select = "<tr><td><select name='companies[]' class='double_short'>"+$("#hidden_select").html()+"</select></td><td><input type='button' class='del' value='Удалить' /></td></tr>";
		$(this).parents("tr:eq(0)").before(select);
	})
	$(".del").live("click",function(){
		var parent = $(this).parents("tr:eq(0)");
		parent.css("background","#faa");
		var answer = confirm("Действительно удалить?");
		parent.css("background","");
		if (answer){
			parent.remove();
		}
	})
})
</script>
<h1>Добавление брокера</h1><br /><?php
	if (count($errors) > 0)
		echo "<p class='fail'>".implode("<br />",$errors)."<br /><br /></p>";
?>
<form method='POST' id='edit'>
	<input type='hidden' name='action' value='add' />
	<table cellpadding='3' cellspacing='5'>
		<tr>
			<td width='170px'><label for='name'>Название</label></td>
			<td width='600px'><input type='text' id='name' name='name' value='<?php echo $route->POST('name'); ?>' /></td>
		</tr>
		<tr>
			<td><label for='city'>Город</label></td>
			<td>
				<select name='city' id='city' class='double_short'><?php
					$cities_list = $db->getTable("select `city_id`,`name` from `city` order by `name`");
					echo select_create($cities_list,"city_id","name",$route->POST('city_id'));
				?></select>
			</td>
		</tr>
		<tr>
			<td><label for='address'>Адрес</label></td>
			<td><input type='text' name='address' id='address' value='<?php echo $route->POST('address'); ?>' /></td>
		</tr>
		<tr>
			<td><label for='phone'>Телефон(ы)</label></td>
			<td><input type='text' name='phone' id='phone' value='<?php echo $route->POST('phone'); ?>' /></td>
		</tr>
		<tr>
			<td><label for='donor'>Страница-донор</label></td>
			<td>
				<input type='text' name='donor' id='donor' value='<?php echo $route->POST('donor'); ?>' />
			</td>
		</tr>
		<tr>
			<td><label for='cookie'>Cookie</label></td>
			<td>
				<input type='text' name='cookie' id='cookie' value='<?php echo $route->POST('cookie'); ?>' />
			</td>
		</tr>
		<tr>
			<td><label for='about'>О брокере</label></td>
			<td><textarea name='about' id='about'><?php echo $route->POST('about'); ?></textarea></td>
		</tr>
		<tr>
			<td>Сотрудничает с</td>
			<td>
				<table>
					<tr><td><input type='button' id='add_company' value="Добавить компанию" /></td></tr>
				</table>
			</td>
		</tr>
		<tr>
			<td colspan='2' align='right'><input type='submit' value='Добавить' /></td>
		</tr>
	</table>
	<select id='hidden_select' style='display:none'><?php
		$companies = $db->getTable("select `company_id`,`name` from `companies` where `head_office` = '0' order by `name`");
		echo select_create($companies,"company_id","name");
	?></select>
</form>

<?php loadPage("footer.php"); ?>